package com.lkd.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.SupplyChannel;
import com.lkd.contract.SupplyContract;
import com.lkd.contract.TaskCompleteContract;
import com.lkd.dao.TaskDao;
import com.lkd.emq.MqttProducer;
import com.lkd.entity.TaskDetailsEntity;
import com.lkd.entity.TaskEntity;
import com.lkd.entity.TaskStatusTypeEntity;
import com.lkd.exception.LogicException;
import com.lkd.feign.UserService;
import com.lkd.feign.VMService;
import com.lkd.http.vo.CancelTaskViewModel;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskDetailsService;
import com.lkd.service.TaskService;
import com.lkd.service.TaskStatusTypeService;
import com.lkd.vo.Pager;
import com.lkd.vo.UserVO;
import com.lkd.vo.VmVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TaskServiceImpl extends ServiceImpl<TaskDao,TaskEntity> implements TaskService{

    @Autowired
    private TaskStatusTypeService statusTypeService;

    @Autowired
    private VMService vmService;

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private TaskDetailsService taskDetailsService;


    @Autowired
    private MqttProducer mqttProducer;//消息生产者


    @Override
    public Pager<TaskEntity> search(Long pageIndex, Long pageSize, String innerCode, Integer userId, String taskCode, Integer status, Boolean isRepair, String start, String end) {
        Page<TaskEntity> page = new Page<>(pageIndex,pageSize);
        LambdaQueryWrapper<TaskEntity> qw = new LambdaQueryWrapper<>();
        if(!Strings.isNullOrEmpty(innerCode)){
            qw.eq(TaskEntity::getInnerCode,innerCode);
        }
        if(userId != null && userId > 0){
            qw.eq(TaskEntity::getUserId,userId);
        }
        if(!Strings.isNullOrEmpty(taskCode)){
            qw.like(TaskEntity::getTaskCode,taskCode);
        }
        if(status != null && status > 0){
            qw.eq(TaskEntity::getTaskStatus,status);
        }
        if(isRepair != null){
            if(isRepair){
                qw.ne(TaskEntity::getProductTypeId, VMSystem.TASK_TYPE_SUPPLY);
            }else {
                qw.eq(TaskEntity::getProductTypeId,VMSystem.TASK_TYPE_SUPPLY);
            }
        }
        if(!Strings.isNullOrEmpty(start) && !Strings.isNullOrEmpty(end)){
            qw
                    .ge(TaskEntity::getCreateTime, LocalDate.parse(start, DateTimeFormatter.ISO_LOCAL_DATE))
                    .le(TaskEntity::getCreateTime,LocalDate.parse(end,DateTimeFormatter.ISO_LOCAL_DATE));
        }
        //根据最后更新时间倒序排序
        qw.orderByDesc(TaskEntity::getUpdateTime);

        return Pager.build(this.page(page,qw));
    }



    @Override
    public List<TaskStatusTypeEntity> getAllStatus() {
        QueryWrapper<TaskStatusTypeEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .ge(TaskStatusTypeEntity::getStatusId,VMSystem.TASK_STATUS_CREATE);

        return statusTypeService.list(qw);
    }
   //新增运维工单
    @Override
    @Transactional
    public boolean createTask(TaskViewModel taskViewModel) throws LogicException {
        //添加工单校验方法(设备编号和设备类型)
        if (hasTask(taskViewModel.getInnerCode(),taskViewModel.getProductType())){
            throw new LogicException("设备存在未完成的工单");
        }
        //1.1获取售货机的地区和区域id
        VmVO vm = vmService.getVMInfo(taskViewModel.getInnerCode());
        //1.2进行机器判断
        if (vm == null){
            throw new LogicException("该机器不存在");
        }
        //1.3进行用户判断
        UserVO user = userService.getUser(taskViewModel.getUserId());
        if (user == null){
            throw new LogicException("该用户不存在");
        }
       //2.新增工单
        TaskEntity taskEntity = new TaskEntity();
        //获取工单编号
        taskEntity.setTaskCode(generateTaskCode());
        //复制熟悉
        BeanUtils.copyProperties(taskViewModel,taskEntity);
        taskEntity.setTaskStatus(VMSystem.TASK_STATUS_CREATE);//工单状态
        taskEntity.setProductTypeId(taskViewModel.getProductType());//工单类型


        //设备
        taskEntity.setAddr(vm.getNodeAddr());//设备地址
        taskEntity.setRegionId(vm.getRegionId());//区域
        taskEntity.setUserName(user.getUserName());//用户名

        this.save(taskEntity);
         //补货工单
        if(taskEntity.getProductTypeId() == VMSystem.TASK_TYPE_SUPPLY){
            taskViewModel.getDetails().forEach(d->{
                TaskDetailsEntity detailsEntity = new TaskDetailsEntity();
                BeanUtils.copyProperties( d,detailsEntity );
                detailsEntity.setTaskId(taskEntity.getTaskId());
                taskDetailsService.save(detailsEntity);
            });
        }
        updateTaskZSet(taskEntity,1);
        return true;
    }
     /*
      *接收工单
      * @author 张梦威
      * @Description
      * @Param [id]
      * @date 2022/6/24 15:12
      */
    @Override
    public boolean accept(Long id) {
        TaskEntity byId = this.getById(id);//查询工单
        if (byId.getTaskStatus()!=VMSystem.TASK_STATUS_CREATE){
            throw new LogicException("工单状态不是待处理");
        }
        byId.setTaskStatus(VMSystem.TASK_STATUS_PROGRESS);//修改工单状态为进行

        return this.updateById(byId);
    }
   //取消工单
    @Override
    public boolean cancelTask(Long id, CancelTaskViewModel cancelVm) {
        TaskEntity task = this.getById(id);
        if (task.getTaskStatus() == VMSystem.TASK_STATUS_FINISH || task.getTaskStatus() ==VMSystem.TASK_STATUS_CANCEL){
           throw  new LogicException("工单已经结束");
        }
        task.setTaskStatus(VMSystem.TASK_STATUS_CANCEL);//工单取消
        task.setDesc(cancelVm.getDesc());
        updateTaskZSet(task,-1);
        return this.updateById(task);
    }


    /**
     * 生成工单编号
     *
     * @return
     */
    private String generateTaskCode() {
        //日期+序号
        String date = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));  //日期字符串
        String key = "lkd.task.code." + date; //redis key
        Object obj = redisTemplate.opsForValue().get(key);
        if (obj == null) {
            redisTemplate.opsForValue().set(key, 1L, Duration.ofDays(1));
            return date + "0001";
        }
        return date + Strings.padStart(redisTemplate.opsForValue().increment(key, 1).toString(), 4, '0');
    }


    /**
     * 创建工单校验
     */
    private void checkCreateTask( Integer vmstatus,int productType)throws LogicException{
        //如果是投放工单，状态不能为运营状态
        if (productType == VMSystem.TASK_TYPE_DEPLOY && vmstatus == VMSystem.VM_STATUS_RUNNING){
            throw new LogicException("设备已在运营");
        }
        //如果是补货工单，状态必须运营状态
        if (productType == VMSystem.TASK_TYPE_SUPPLY && vmstatus == VMSystem.VM_STATUS_RUNNING){
            throw new LogicException("该设备不在运营状态");
        }
        //如果是撤机工单，状态必须运营状态
        if (productType == VMSystem.TASK_TYPE_REVOKE && vmstatus == VMSystem.VM_STATUS_RUNNING){
            throw new LogicException("该设备不在运营状态");
        }

    }

    //完成工单
    @Override
    public boolean completeTask(long id) {
        TaskEntity taskEntity = this.getById(id);
        if(taskEntity.getTaskStatus()== VMSystem.TASK_STATUS_FINISH  || taskEntity.getTaskStatus()== VMSystem.TASK_STATUS_CANCEL ){
            throw new LogicException("工单已经结束");
        }
        taskEntity.setTaskStatus(VMSystem.TASK_STATUS_FINISH);//修改工单状态
        this.updateById(taskEntity);
        //判断工单类型，对运维工单进行封装处理
        if (taskEntity.getProductTypeId() == VMSystem.TASK_TYPE_DEPLOY
                || taskEntity.getProductTypeId() == VMSystem.TASK_TYPE_REVOKE){
             noticeVMServiceStatus(taskEntity);
        }
        //如果是补货工单
        if (taskEntity.getProductTypeId() == VMSystem.TASK_TYPE_SUPPLY){
            noticeVMServiceSupply(taskEntity);
        }
        return true;
    }


    //获取工单最少id
    @Override
    public int getLeastUser(Long regionId, Boolean isRepair) {
        String roleCode="1002";
        if(isRepair){ //如果是运维工单
            roleCode="1003";
        }
        String key= VMSystem.REGION_TASK_KEY_PREF
                + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
                +"."+ regionId+"."+roleCode;
        Set<Object> set = redisTemplate.opsForZSet().range(key, 0, 0);//取第1个
        if(set==null || set.isEmpty()){
            return 0;  //如果没有数据则返回0
        }
        return (Integer)set.stream().collect( Collectors.toList()).get(0) ;
    }


    /**
     * 同一台设备下是否存在未完成的工单
     */
    private boolean hasTask(String innerCode,int productionType){
       /* QueryWrapper<TaskEntity> qw = new QueryWrapper<>();
        qw.lambda()
                .select(TaskEntity::getTaskId)//工单id
                .eq(TaskEntity::getInnerCode,innerCode)//设备编号
                .eq(TaskEntity::getProductTypeId,productionType)//设备类型
                .le(TaskEntity::getTaskStatus,VMSystem.TASK_STATUS_PROGRESS);*/

        LambdaQueryWrapper<TaskEntity> zmw = new LambdaQueryWrapper<>();
        zmw.eq(TaskEntity::getInnerCode,innerCode)//设备编号
                .eq(TaskEntity::getProductTypeId,productionType)//设备类型
                .le(TaskEntity::getTaskStatus,VMSystem.TASK_STATUS_PROGRESS);
        return this.count(zmw) > 0;
    }



    /**
     * 运维工单封装与下发
     * @param taskEntity
     */
    private void noticeVMServiceStatus(TaskEntity taskEntity){
        //向消息队列发送消息，通知售货机更改状态
        //封装协议
        TaskCompleteContract taskCompleteContract=new TaskCompleteContract();
        taskCompleteContract.setInnerCode(taskEntity.getInnerCode());//售货机编号
        taskCompleteContract.setTaskType( taskEntity.getProductTypeId() );//工单类型
        //发送到emq
        try {
            mqttProducer.send( TopicConfig.VMS_COMPLETED_TOPIC,2, taskCompleteContract );
        } catch (Exception e) {
            log.error("发送工单完成协议出错");
            throw new LogicException("发送工单完成协议出错");
        }
    }

/**
 * 补货协议封装与下发
 * @param taskEntity
 */
   private void noticeVMServiceSupply(TaskEntity taskEntity){
       //协议内容封装
       //1.根据工单id查询工单明细表
       LambdaQueryWrapper<TaskDetailsEntity> qw = new LambdaQueryWrapper<>();
       qw.eq(TaskDetailsEntity::getTaskId,taskEntity.getTaskId());
       List<TaskDetailsEntity> details = taskDetailsService.list(qw);
       //2.构建协议内容
       SupplyContract supplyContract = new SupplyContract();
       supplyContract.setInnerCode(taskEntity.getInnerCode());//售货机编号
       ArrayList<SupplyChannel> supplyChannels = Lists.newArrayList();//补货数据
       //从工单明细表提取到捕获数据中
       details.forEach(d->{
           SupplyChannel channel = new SupplyChannel();
           channel.setChannelId(d.getChannelCode());//货道编号
           channel.setCapacity(d.getExpectCapacity());//补货数量
           supplyChannels.add(channel);
       });
       supplyContract.setSupplyData(supplyChannels);

       //3.下发补货协议
       //发送到emq
       try {
           mqttProducer.send( TopicConfig.VMS_COMPLETED_TOPIC,2, supplyContract );
       } catch (Exception e) {
           log.error("发送工单完成协议出错");
           throw new LogicException("发送工单完成协议出错");
       }

   }

 /*
  *更新工单量列表
  * @author 张梦威
  * @Description
  * @Param [taskEntity, score]
  * @date 2022/6/26 11:24
  */
   private void updateTaskZSet(TaskEntity taskEntity, int score){
       //定义人员代码
       String roleCode = "1003";//运维
       if (taskEntity.getProductTypeId().intValue() ==2){//判断工单类型为补货
           roleCode = "1002";//转为运营

       }
       //定义格式redis数据key
       String key =VMSystem.REGION_TASK_KEY_PREF
               + LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))
               + "."+ taskEntity.getRegionId()+"."+roleCode;
       redisTemplate.opsForZSet().incrementScore(key,taskEntity.getUserId(),score);
   }

}
